package com.lsh.oms.core.service.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.constant.ShortMessageConstants;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.proxy.service.MisProxy;
import com.lsh.oms.core.service.asynservice.OmsEventBus;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name: lsh-groupon
 *
 * @author peter
 * 北京链商电子商务有限公司
 * Desc: 类功能描述
 * Package Name: com.lsh.groupon.core.ShortMessageService
 * Time: 2017-07-04 上午11:01
 */
@Service
public class ShortMessageHandler {

    private static final Logger logger = LoggerFactory.getLogger(ShortMessageHandler.class);

    @Autowired
    private MisProxy misProxy;

    public void pushNotify(OrderHead orderHead) {

        if (null == orderHead) {
            return;
        }

        String headExt = orderHead.getExt();
        if (org.springframework.util.StringUtils.isEmpty(headExt)) {
            return;
        }

        try {
            JSONObject headExtJson = JSON.parseObject(headExt);

            if (null == headExtJson) {
                return;
            }

            String providerId = headExtJson.getString("providerId");
            String providerPhone = misProxy.requestProviderPhone(providerId, orderHead.getVenderId());

            if (StringUtils.isBlank(providerPhone)) {
                logger.info(orderHead.getOrderCode() + " : providerPhone is null");
                return;
            }

            ShortMessageService.ShortMessage shortMessage = new ShortMessageService().new ShortMessage();
            shortMessage.setContent(ShortMessageConstants.PROVIDER_MSG);
            shortMessage.setTo(providerPhone);

            logger.info(orderHead.getOrderCode() + " :shortMessage is " + JSON.toJSONString(shortMessage));
            OmsEventBus.post(shortMessage);
        } catch (Exception e) {
            logger.error("短信通知供商失败", e);
        }
    }
}
